const axios = require('axios');

// 测试课程详情API
async function testCourseDetail() {
  try {
    const courseId = 11;
    
    console.log(`测试获取课程ID: ${courseId} 的详情`);
    
    // 获取课程数据
    const courseResponse = await axios.get(`http://localhost:3002/api/demo/getsingle?id=${courseId}&typeIndex=6`);
    console.log('API返回的完整响应:');
    console.log(JSON.stringify(courseResponse.data, null, 2));
    
    // 检查数据结构
    if (courseResponse.data && courseResponse.data.success && courseResponse.data.data) {
      console.log('\n✅ 数据结构正确，包含success和data字段');
      const courseData = courseResponse.data.data;
      
      console.log('\n课程核心信息:');
      console.log(`课程名称: ${courseData.name}`);
      console.log(`老师姓名: ${courseData.teacher}`);
      console.log(`课程介绍: ${courseData.description}`);
      console.log(`创建时间: ${courseData.createdAt}`);
      
      // 检查所有必要字段是否存在
      const requiredFields = ['id', 'name', 'description', 'teacher', 'createdAt'];
      const missingFields = requiredFields.filter(field => !(field in courseData));
      
      if (missingFields.length === 0) {
        console.log('\n✅ 所有必要字段都存在');
      } else {
        console.log(`\n❌ 缺少以下必要字段: ${missingFields.join(', ')}`);
      }
    } else {
      console.log('\n❌ 数据结构不正确，缺少success或data字段');
    }
    
    // 测试获取任务项
    const tasksResponse = await axios.get(`http://localhost:3002/api/demo/getallitems?typeIndex=7&courseId=${courseId}`);
    console.log('\n任务项API返回:');
    console.log(`任务项数量: ${tasksResponse.data.data ? tasksResponse.data.data.length : 0}`);
    
    // 测试获取评价
    const commentsResponse = await axios.get(`http://localhost:3002/api/demo/getallitems?typeIndex=4&articleid=${courseId}`);
    console.log('\n评价API返回:');
    console.log(`评价数量: ${commentsResponse.data.data ? commentsResponse.data.data.length : 0}`);
    
  } catch (error) {
    console.error('\n❌ 测试过程中发生错误:', error.message);
    if (error.response) {
      console.error('响应状态:', error.response.status);
      console.error('响应数据:', error.response.data);
    }
  }
}

testCourseDetail();